/**
 * 
 */
package ro.dta.idbi.ui;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

import ro.dta.idbi.interfaces.ui.IRightPanel;

/**
 * Panel displayed on the right of the main application
 * 
 * @author Tudor Dobrila
 * 
 */
public class RightPanel extends JGradientPanel implements IRightPanel {

	private static final long serialVersionUID = -8904885632578794737L;

	private static JLabel loadingLabel = new JLabel(new ImageIcon("./resources/loader.gif"));

	protected transient JTitleLabel titleLabel;

	protected transient JPanel contentPanel;

	/**
	 * Right panel class constructor
	 * 
	 * @param initialTitle
	 *            Initial title of the panel
	 * @param initialPanel
	 *            Initial content shown in the panel
	 */
	public RightPanel(final String initialTitle, final JPanel initialPanel) {
		super();
		this.contentPanel = initialPanel;

		setLayout(new GridBagLayout());
		final GridBagConstraints constr = new GridBagConstraints();
		constr.fill = GridBagConstraints.HORIZONTAL;
		constr.anchor = GridBagConstraints.NORTH;
		constr.weightx = 1.0;
		constr.weighty = 0.0;
		constr.gridx = 0;
		constr.gridy = 0;
		constr.ipady = 15;
		titleLabel = new JTitleLabel(initialTitle);
		add(titleLabel, constr);

		if (contentPanel != null) {
			constr.weighty = 1.0;
			constr.ipady = 0;
			constr.gridx = 0;
			constr.gridy = 1;
			add(contentPanel, constr);
		}
	}

	@Override
	public void setTitle(final String title) {
		titleLabel.setText(title);
	}

	@Override
	public void setContent(final JPanel newPanel, final int fill) {
		if (newPanel == null) {
			return;
		}

		hideLoading();

		if (contentPanel != null) {
			remove(contentPanel);
		}

		contentPanel = newPanel;

		final GridBagConstraints constr = new GridBagConstraints();
		constr.fill = fill;
		constr.anchor = GridBagConstraints.NORTH;
		constr.weighty = 1.0;
		constr.weightx = 1.0;
		constr.gridx = 0;
		constr.gridy = 1;
		add(newPanel, constr);

		validate();
	}

	@Override
	public void showLoading() {
		if (contentPanel != null) {
			remove(contentPanel);
		}

		final GridBagConstraints constr = new GridBagConstraints();
		constr.fill = GridBagConstraints.BOTH;
		constr.anchor = GridBagConstraints.NORTH;
		constr.weighty = 1.0;
		constr.weightx = 1.0;
		constr.gridx = 0;
		constr.gridy = 1;

		add(loadingLabel, constr);

		validate();
	}

	@Override
	public void hideLoading() {
		remove(loadingLabel);
		validate();
	}

	@Override
	public JPanel getContent() {
		return contentPanel;
	}
}
